#include "sys_data.h"

sys_Data * sys_Data::ptr_sysData = nullptr;

sys_Data *sys_Data::getInstance()
{
    if(nullptr == sys_Data::ptr_sysData) {
        return sys_Data::ptr_sysData = new sys_Data;
    } else {
        return sys_Data::ptr_sysData;
    }
}

QStringList sys_Data::Theme = QStringList({
    "Default",
    "ElegantDark",
    "Ubuntu"
});

QStringList sys_Data::Shell = QStringList({
    "cmd",
    "powershell"
});

sys_Data::sys_Data():
    m_setting(new QSettings("./config.ini", QSettings::IniFormat))
{
    if(this->m_setting->value("maxOpenFileHistoryRecord").toInt() == 0) {
        this->m_setting->setValue("maxOpenFileHistoryRecord", 3);
    }
    if(this->m_setting->value("themePath").toString().isEmpty()) {
        this->m_setting->setValue("themePath", sys_Data::Theme.first());
    }
    if(this->m_setting->value("shell").toString().isEmpty()) {
        this->m_setting->setValue("shell", sys_Data::Shell.first());
    }
    if(this->m_setting->value("condaEnvironment").toString().isEmpty()) {
        this->m_setting->setValue("condaEnvironment", "");
    }
    if(this->m_setting->value("analysisAlgPath").toString().isEmpty()) {
        this->m_setting->setValue("analysisAlgPath", "");
    }
}

sys_Data::~sys_Data()
{
    this->m_setting->sync();
    delete m_setting;
}

QString sys_Data::getThemePath()
{
    return this->m_setting->value("themePath").toString();
}

void sys_Data::setThemePath(const QString &path)
{
    this->m_setting->setValue("themePath", path);
}

QString sys_Data::getThemeQss()
{
    QFile file(QString(":/res/qss/%1.qss").arg(sys_Data::getInstance()->getThemePath()));
    if(file.exists() && file.open(QIODevice::ReadOnly)) {
        return file.readAll();
    } else {
        return "";    //返回后file会执行析构自己关闭文件
    }
}

int sys_Data::getMaxOpenFileHistoryRecord()
{
    return this->m_setting->value("maxOpenFileHistoryRecord").toInt();
}

void sys_Data::setMaxOpenFileHistoryRecord(int num)
{
    this->m_setting->setValue("maxOpenFileHistoryRecord", num);
}

QStringList sys_Data::getOpenFileHistory()
{
    QString record = this->m_setting->value("openFileHistoryRecord").toString();
    if(record.isEmpty()) {
        return QStringList();
    } else {
        return record.split('|');
    }
}

void sys_Data::addOpenFileHistory(const QString &path)
{
    QFileInfo info(path);
    this->m_setting->setValue("openDirHistory", info.absolutePath());//记录该文件的path目录
    QStringList list = this->getOpenFileHistory();//获取历史记录
    int index = list.indexOf(path);
    if(-1 == index) {//新纪录
        list.push_front(path);
        if(list.size() > this->m_setting->value("maxOpenFileHistoryRecord").toInt()) {//如果超过了记录最大值
            list.removeLast();//删除最后一条记录
        }
    } else {//旧纪录
        list.move(index, 0);//移动到记录第一条
    }
    this->m_setting->setValue("openFileHistoryRecord", list.join('|'));
}

QString sys_Data::getOpenDirHistory()
{
    return this->m_setting->value("openDirHistory").toString();
}

QString sys_Data::getShell()
{
    return this->m_setting->value("shell").toString();
}

void sys_Data::setShell(const QString &shell)
{
    this->m_setting->setValue("shell", shell);
}

QString sys_Data::getCondaEnvir()
{
    return this->m_setting->value("condaEnvironment").toString();
}

void sys_Data::setCondaEnvir(const QString &condaEnvir)
{
    this->m_setting->setValue("condaEnvironment", condaEnvir);
}

QString sys_Data::getAnalysisAlgPath()
{
    return this->m_setting->value("analysisAlgPath").toString();
}

void sys_Data::setAnalysisAlgPath(const QString &analysisAlgPath)
{
    return this->m_setting->setValue("analysisAlgPath", analysisAlgPath);
}
